import static org.junit.Assert.*;

import java.util.Random;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;


public class UrlRandTest {

	@Before
	public void setUp() throws Exception {
	}

	@After
	public void tearDown() throws Exception {
	}

	@Test
	public void test() {
		  UrlValidator urlVal = new UrlValidator(null, null, UrlValidator.ALLOW_ALL_SCHEMES);
		int min = 0; 
		int max = 93; 
		String base; 
				
		for (int i = 0; i < 10000; ++i){
			   int a = randInt(min, max);
			   base = testHelper2(a);
			   
			   String[] prefix = {"http://", "https://", "http://www.", "https://www.", "www.", ""};
			   
			   String[] suffix = {".com", ".org", ".edu", ".net", ".gov", ".mil", ".co.ca", ".co.uk", ".co.jp",".ca", ".uk", ".jp" };
			   
			   //Loop through prefixes
			   for(int j = 0; j < 6; j++){
				   //Loop through suffixes
				   for(int k = 0; k < 12; k++){
					   String testURL;
					   testURL = prefix[j] + base + suffix[k];
					   if (urlVal.isValid(testURL))
					   {
						   System.out.println(testURL + "passed test");
					   }
					   else 
					   {
						   System.out.println(testURL + "FAILED!!!!!!!");
					   }
				   }
			   	}
			}
	}
	

/////function to create the base	
	public String testHelper2(int s){
		char [] testArray; 
		testArray = findAscii();	
		int min = 0; 
		int max  = 93;
		char [] stringArray = new char[s];
		for (int i = 0; i < s; ++i){
			int a = randInt(min, max);
			stringArray[i] = testArray[a];
		}
	String st = new String(stringArray);
	return st;
	}
	
////////////creates an array of all the ascii values
	public char[] findAscii(){
		
		char[] anArray;
		anArray = new char[94];
		int i=33;
		
		for (int j = 0; j < 94; ++j){
			{
				System.out.println("Code: " + i + " Character: " +(char)i);
				anArray[j] = (char)i;
				++i;
			}	
		}
		
		return anArray;
	
	}
	
	 public void testIsValid()
	   {
		   UrlValidator urlVal = new UrlValidator(null, null, UrlValidator.ALLOW_ALL_SCHEMES);
		   String base;
		   int min = 1; 
		   int max = 50; 
		   for (int i = 0; i < 10000000; ++i){
			   int a = randInt(min, max);
			   base = testHelper2(a);
			   
			   String[] prefix = {"http://", "https://", "http://www.", "https://www.", "www.", ""};
			   
			   String[] suffix = {".com", ".org", ".edu", ".net", ".gov", ".mil", ".co.ca", ".co.uk", ".co.jp",".ca", ".uk", ".jp" };
			   
			   //Loop through prefixes
			   for(int j = 0; j < 6; j++){
				   //Loop through suffixes
				   for(int k = 0; k < 12; k++){
					   String testURL;
					   testURL = prefix[j] + base + suffix[k];
					   if (urlVal.isValid(testURL))
					   {
						   System.out.println(testURL + "passed test");
					   }
					   else 
					   {
						   System.out.println(testURL + "FAILED!!!!!!!");
					   }
				   }
			   	}
			  
		  }
	 }   
	 
	 
	 public static int randInt(int min, int max) {

		    // Usually this should be a field rather than a method variable so
		    // that it is not re-seeded every call.
		    Random rand = new Random();

		    // nextInt is normally exclusive of the top value,
		    // so add 1 to make it inclusive
		    int randomNum = rand.nextInt((max - min) + 1) + min;

		    return randomNum;
		}
	 
}
